package demo2;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: DELL
 * Date: 2023-08-05
 * Time: 16:56
 */

class Count {
    public volatile int count = 0;

    public void add2() {
        add();
    }

    public void add() {

            count++;
    }
}

public class thread1 {

    public static void main(String[] args) {
        Count count = new Count();

        Thread t1 = new Thread(() -> {
            for (int i = 0; i < 50_000; i++) {
                count.add();
            }
        });

        Thread t2 = new Thread(() -> {
            for (int i = 0; i < 50_000; i++) {
                count.add();
            }
        });

        t1.start();
        t2.start();

        try {
            t1.join();
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        }
        try {
            t2.join();
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        }

        System.out.println(count.count);
    }
}
